//
//  ShopSettingTableViewCell.m
//  Findif
//
//  Created by Mac on 2018/11/12.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopSettingTableViewCell.h"

@interface ShopSettingTableViewCell ()

@property (nonatomic, weak)UIView * lineView;

@end

@implementation ShopSettingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
        
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ZRWhiteColor;//ColorWithRGB(0xEFF0F1);
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.iconImageView = iconImageView;
    
    [self.contentView addSubview:self.iconImageView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    titleLabel.text = @"----";
    titleLabel.textAlignment = NSTextAlignmentLeft;
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    self.rightImageView = rightImageView;
    
    [self.contentView addSubview:self.rightImageView];
    
    
    UILabel * subLabel = [UILabel new];
    
    subLabel.textColor = [UIColor lightGrayColor];
    subLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    subLabel.text = @"----";
    subLabel.textAlignment = NSTextAlignmentRight;
    
    self.subLabel = subLabel;
    
    [self.contentView addSubview:self.subLabel];
    
    
    UISwitch * billSwitch = [UISwitch new];
    
    billSwitch.on = YES;
    [billSwitch setTintColor:ColorWithRGB(0xeeeeee)];
    [billSwitch setOnTintColor:ColorWithRGB(0xFFE055)];
    [billSwitch setThumbTintColor:ZRWhiteColor];
    billSwitch.backgroundColor = ColorWithRGB(0xeeeeee);
    billSwitch.layer.cornerRadius = 15.5 * SCREENH6;
    billSwitch.layer.masksToBounds = YES;
    [billSwitch addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventValueChanged];
    self.billSwitch = billSwitch;
    
    [self.contentView addSubview:self.billSwitch];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xE4E4E4);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}

- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.contentView).offset(10 * SCREENH6);
        make.height.with.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.trailing.mas_equalTo(self.contentView).offset(-10 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    [self.billSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-15 * SCREENH6);
//        make.leading.mas_equalTo(self.titleLabel.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(195 * SCREENH6);
        make.centerY.mas_equalTo(self.rightImageView);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1 * SCREENH6);
        make.leading.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self.rightImageView);
    }];
}


- (void)switchAction{
    //判断开关的状态
    if (self.billSwitch.on) {
                NSLog(@"switch is on");
        
        [[ZRNetworkTool sharedTool] editMerchantsResultWithMerchants_status:@"1" WithBlock:^(BOOL success, id responseData) {
            
//            NSLog(@"res===%@",responseData);
            if (success) {
                
                self.titleLabel.text = @"营业中";
            }
        }];
        
    } else {
                NSLog(@"switch is off");
        
        [[ZRNetworkTool sharedTool] editMerchantsResultWithMerchants_status:@"0" WithBlock:^(BOOL success, id responseData) {
            
//            NSLog(@"res===%@",responseData);
            if (success) {
                
                self.titleLabel.text = @"休息中";
            }
        }];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
